package app

import (
	"go-demo/common/global"
	"go.etcd.io/etcd/client/v3"
	"log"
	"time"
)

type Etcd struct {
}

func (e Etcd) Load() {
	global.App.Etcd = connectEtcd()
}

func connectEtcd() *clientv3.Client {
	var (
		address = global.App.Config.EtcdConfig.Address
		timeout = global.App.Config.EtcdConfig.Timeout
	)

	client, err := clientv3.New(clientv3.Config{
		Endpoints:   []string{address},
		DialTimeout: time.Duration(timeout) * time.Second,
	})

	if err != nil {
		log.Panicf("connect to etcd failed, err:%v\n\n", err)
	}
	log.Println("connect to etcd success ", client)

	return client
}
